﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp_homework_3
{
    //闹钟类----------------------------------------------------------题目2-------------------------------------------------------------------
    public class Clock
    {
        private DateTime alarm;                      //DateTime表示时间上的一刻,alarm为设定的事件

        public delegate void AlarmHandler(object sender, DateTime d);             //和响铃alarm有关的事件类型
        public delegate void TickHandler(object sender, DateTime d);             //和嘀嗒Tick有关的事件类型

        public event AlarmHandler Clock_Alarm;                                          //Alarm事件的声明
        public event TickHandler Clock_Tick;                                            //Tick事件的声明

        public void Tick(object sender,DateTime time)                                    //Tick事件处理方法
        {
            Console.WriteLine("现在时间为："+time);                                       //输出时间
        }
        public void Alarm(object sender, DateTime time)                                    //Alarm事件处理方法
        {
            Console.WriteLine("设定时间到了，现在时间为：" + time);                                       //输出时间
        }

        public Clock()                    //构造函数，每次初始化就把Alarm,Tick事件注册
        {
            Clock_Alarm += Alarm;
            Clock_Tick += Tick;
        }

        public void running()                                                                   //跑起来
        {
            while(true){                                           //就是让时间能跑起来，不然只输出一次
                DateTime now = DateTime.Now;                       //把本地时间赋给now
                Clock_Tick(this, now);                                                               //每时每刻都触发Tick
                if (now.ToString() == alarm.ToString()) { Clock_Alarm(this, alarm); }                //设定时间到了，就触发一次
                System.Threading.Thread.Sleep(1000); 
            }
        }

        public void SetAlarm(DateTime alarmTime)                                                   //设定 设定时间
        {
            Console.WriteLine("设定时间为："+alarmTime);
            alarm = alarmTime;
        }

    }

    //链表结点----------------------------------------------------------PPT上的-------------------------------------------------------------------
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }

        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }

    //泛型链表类
    public class Genericlist<T>
    {
        private Node<T> head;
        private Node<T> tail;

        public Genericlist()
        {
            tail = head = null;
        }

        public Node<T> Head
        {
            get => head;
        }

        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null) { head = tail = n; }
            else { tail.Next = n;tail = n; }
        }

        //ForEach方法-------------------------------------------------题目1---------------------------------------------------------------------
        public void ForEach(Action<T> action)
        {
                //Genericlist<T> Tlist = new Genericlist<T>();
                for (Node<T> node = Head; node != null; node = node.Next)                         
            {
                action(node.Data);                                                             
            }
        }

    }

    class Program
    {
            static void Main(string[] args)
            {
                /*//整型List
                Genericlist<int> intlist = new Genericlist<int>();
                for(int x = 0; x < 10; x++)
                {
                    intlist.Add(x);
                }
                for (Node<int> node = intlist.Head; node != null; node = node.Next)
                {
                    Console.WriteLine(node.Data);
                }

                //字符串型List
                Genericlist<string> strlist = new Genericlist<string>();
                for (int x = 0; x < 10; x++)
                {
                    strlist.Add("str"+x);
                }
                for (Node<string> node = strlist.Head; node != null; node = node.Next)
                {
                    Console.WriteLine(node.Data);
                }*/

                //打印、求和、求最大最小值-------------------------------------------------题目1输出----------------------------------------------
                int sum = 0;                                              //sum为和

                Genericlist<int> intlist = new Genericlist<int>();                                   //添加数字
                for (int x = 0; x < 10; x++)
                {
                    intlist.Add(x);
                }

                int max = intlist.Head.Data;                                //最大值，暂时先等于头节点的值
                int min = intlist.Head.Data;                                //最小值，同理

                intlist.ForEach(i => Console.WriteLine(i));                //就自己往下循环了，事件简化写法
                intlist.ForEach(i => sum = sum + i);                       //求和
                intlist.ForEach(i => { min = min > i ? i : min; });          //求最小
                intlist.ForEach(i => { max = max < i ? i : max; });          //求最大

                Console.WriteLine("最大值为："+ max);                        //输出数字还可以这么输，输出最大值
                Console.WriteLine("最小值为：" + min);                           //输出最小值
                Console.WriteLine("和为：" + sum);                             //输出和

            //使用事件机制，模拟实现有一个闹钟功能----------------------------------------------题目2---------------------------------------------
            Clock clock = new Clock();                         //创建一个Clock类的对象
            DateTime time = new DateTime();
            time = DateTime.Now.AddSeconds(5);                   //返回一个新的Datetime,将5s加到实例的值上
            clock.SetAlarm(time);
            clock.running();

            //Console.WriteLine("!");                             
        }
    }
}
